<?php
use \wechat\OscshopWechat;
use \think\Db;

if (!function_exists('wechat')) {
    /**
     * 助手函数
     */
    function wechat()
    {
    	return OscshopWechat::getInstance([
    	'appid'=>config('appid'),
    	'appsecret'=>config('appsecret'),
    	'token'=>config('token'),
    	'encodingaeskey'=>config('encodingaeskey')]);		
    }
}

//通过地区的名称取地区的id
function get_area_id_by_name($area_name){
	$list = Db::name('area')
			->where('area_name',$area_name)
			->field('area_id')
			->find();
	if($list){
		return $list['area_id'];
	}
	return [];


	if (!$area_list = cache('area_name_list')) {

		$list=Db::name('area')->field('area_id,area_name')->select();

		foreach ($list as $k => $v) {
			$area[$v['area_name']]=$v;
		}
		cache('area_name_list', $area);	
		
		$area_list=$area;

	}
	return $area_list[$area_name]['area_id'];
	
}
//小数转百分比
function num_to_percent($n)
{	
	return ($n*100)."%";
} 
//取代理级别信息
function get_agent_level_info($al_id,$key)
{	
	$agent="";
	if (!$agent_level = cache('agent_level')) {
		
		$list=Db::name('agent_level')->select();
		
		foreach ($list as $k => $v) {
			$agent[$v['al_id']]=$v;
		}
		cache('agent_level', $agent);	
		$agent_level=$agent;
	}
	if(isset($agent_level[$al_id][$key])){
		return $agent_level[$al_id][$key];
	}
	return null;	
} 
//取得用户信息
function get_member_info($uid,$key){
	$user=Db::name('member')->where('uid',$uid)->find();
	
	if(!$uid){
		return null;
	}
	return $user[$key];
}

function deal_agent_share(){
	$sharemobile=hashids_decode(input('param.osc_aid'));
	session('sharemobile',$sharemobile);
	
	
	/*
	if(in_wechat()){	
		//代理商的ID		
		$pid=hashids_decode(input('param.osc_aid'));
		$uid=(int)member('uid');
		if($pid&&$uid){				
			$mem=Db::name('member')->where('uid',$uid)->find();		
			if($mem['pid']==0){
				Db::name('member')->where('uid',$uid)->update(['pid'=>$pid]);
			}
		}
	}
	*/
}

/**
 * 获取用户真实姓名
 * @param $uid
 * @return mixed
 */
function get_id_card_name($uid){
	$name = Db::name('member')->where('uid',$uid)->find();
	return $name['idcardname'];
}

/**
 * 获取用户真实姓名
 * @param  $username
 * @return mixed
 */
function get_username_card_name($username){
	$name = Db::name('member')->where('username',$username)->find();
	return $name['idcardname'];
}

/**
 * 获取用户昵称
 * @param  $username
 * @return mixed
 */
function get_username_nickname($username){
	$name = Db::name('member')->where('username',$username)->find();
	return $name['nickname'];
}

/**
 * 只保留字符串首尾字符，隐藏中间用*代替（两个字符时只显示第一个）
 * @param string $user_name 姓名
 * @return string 格式化后的姓名
 */
function substr_cut($user_name){
	$strlen     = mb_strlen($user_name, 'utf-8');
	$firstStr     = mb_substr($user_name, 0, 1, 'utf-8');
	$lastStr     = mb_substr($user_name, -1, 1, 'utf-8');
	return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
}

/**
 * 查询规格名称
 * @param $attribute_value_id
 * @return mixed
 */
function get_value_name($attribute_value_id){
	$res = Db::name('attribute_value')->where('attribute_value_id',$attribute_value_id)->find();
	return $res['value_name'];
}

/**
 * 查询选中的规格的价格
 * @param $goods_id
 * @param $attribute_value_id
 * @return mixed
 */
function get_attribute_money($goods_id,$attribute_value_id){
	$res = Db::name('goods_attribute')->where(['goods_id'=>$goods_id,'attribute_value_id'=>$attribute_value_id])->find();
	return $res['money'];
}

/**获取交易所名称
 * @param $trade_id
 * @return mixed
 */
function get_trade_name($trade_id){
	$res = Db::name('exchange_house')->where('id',$trade_id)->find();
	return $res['name'];
}

/**
 * 获取交易品种名称
 * @param $variety_id
 * @return mixed
 */
function get_variety_name($variety_id){
	$res = Db::name('exchage_variety')->where('id',$variety_id)->find();
	return $res['name'];
}

/**
 * 获取周期
 * @param $time_id
 * @return string
 */
function get_contract_time($time_id){
	$time = Db::name('contract_time_config')->where('id',$time_id)->find();
	$type = get_contract_time_type($time['type']);
	return $time['num'].$type;
}

/**
 * 获取周期单位
 * @param $type
 * @return string
 */
function get_contract_time_type($type){
	if($type == 1){
		return '天';
	}elseif($type == 2){
		return '月';
	}elseif($type == 3){
		return '年';
	}
}


/**
 * 获取合约名称
 * @param $info_id
 * @return mixed
 */
function get_contract_info_name($info_id){
	$res = Db::name('contract_info')->where('id',$info_id)->find();
	return $res['name'];
}

/**
 * 根据交易品种获取最新交易价格
 * @param $exchange_id
 * @param $variety_id
 * @return mixed
 */
function get_value_max($exchange_id,$variety_id){
	$value_max = Db::name('value_config')->where(['exchange_id'=>$exchange_id,'variety_id'=>$variety_id])->order('dt desc')->find();
	return $value_max['rate'];
}

/**
 * 根据交易品种获取发行价
 * @param $variety_id
 * @return mixed
 */
function get_contract_issue($variety_id){
	$issue = Db::name('contract_issue')->where(['variety_id'=>$variety_id])->find();
	return $issue['issue'];
}

/**
 * 根据交易品种获取目标价
 * @param $variety_id
 * @return mixed
 */
function get_contract_target($variety_id){
	$issue = Db::name('contract_issue')->where(['variety_id'=>$variety_id])->find();
	return $issue['target'];
}

/**
 * 根据订单和订单类型获取交易所id或者交易品种id
 * @param $day
 * @param $contract
 * @param $type
 * @return mixed
 */
function get_username_contract_type($day,$contract,$type){
	if($type == 0){
		$one = Db::name('contract_b1')->where(['id'=>$contract])->find();
		if($day == 1){
			return $one['exchange'];
		}else{
			return $one['exchange_variety'];
		}
	}else{
		$two = Db::name('contract_b2')->where(['id'=>$contract])->find();
		if($day == 1){
			return $two['exchange'];
		}else{
			return $two['exchange_variety'];
		}
	}
}

/**
 * 合约类型判断名称
 * @param $type
 * @return string
 */
function get_contract_type_name($type){
	if($type == 0){
		$res = '自由买卖';
	}elseif($type == 1){
		$res = '目标价认购';
	}elseif($type == 2){
		$res = '新自由合约';
	}else{
		$res = '无合约';
	}
	return $res;
}

/**
 * 获取匹配金额后的利率及金额
 * @param $member
 * @return array|float|int
 */
function get_release_rate($member){

	$rate = intval(($member-0.01)/10000)*0.005+0.01;

	if($rate > 1 || $member == 0.01){
		$rate = 1;
	}
	$money = round($member*$rate,2);

	if($member < $money || $money == 0){
		$num = $member;
	}else{
		$num = $money;
	}

	return ['rate'=>$rate,'num'=>$num];
}


?>
